<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Validate;
use fast\Random;
use app\common\library\Token;
use fast\Http;
use think\Log;
class Xcxuser extends Api
{
  // 无需登录的接口,*表示全部
  protected $noNeedLogin = ['login','getopenid'];
  // 无需鉴权的接口,*表示全部
  protected $noNeedRight = ['ceshi'];

  protected $AppID = "wx2f955f731f9a3689";
  protected $AppSecret = "d5a9830f342346061495c6c4b645728c";

  public function getopenid($code)
  {
      $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$this->AppID&secret=$this->AppSecret&js_code=$code&grant_type=authorization_code";

      $res = Http::get($url);
      $res = json_decode($res,true);
      // $this->error('登录失败：' ,["data"=>$res]);
      Log::info($res);
      if($res["openid"]){

        $post = $this->request->post();

        

        $u = model('admin/User')->where('openid',$res["openid"])->find();
        if($u){
          // 执行登录
          Token::clear($u["id"]);
          $this->auth->direct($u["id"]);
          $this->success('登录成功', $this->auth->getUserinfo());
        }
        else{
          $rule = [
            'nickname'  => 'require|length:2,30'
        ];
        
        $msg = [
            'nickname.require' => '缺少nickname',
            'nickname.length' => 'nickname长度不符合',
        ];
        
        $v = new Validate($rule,$msg);

        
        if (!$v->check($post)) {
            $this->error('登录失败：' . $v->getError());
        }
          // 执行注册
          $username = $res["openid"];
          // 初始密码给一个随机数
          $password = Random::alnum(15);
          $this->auth->register($username,$password,'','',[
              "username"=>$post["nickname"],
              "nickname"=>$post["nickname"],
              "avatar"=>$post["avatar"],
              "gender"=>$post["gender"] == 1?'0':'1',
              "openid"=>$res["openid"],
          ]);
          $this->success('注册成功', $this->auth->getUserinfo());

        }

        $this->success('cc',$res);
      }
      else{
        $this->error('cc',$res);

      }
  }
 
  public function login()
  {
    $post = $this->request->post();

    $rule = [
        'openid'  => 'require|length:10,30'
    ];
    
    $msg = [
        'openid.require' => '缺少openid',
        'openid.length' => 'openid长度不符合',
    ];
    
    $v = new Validate($rule,$msg);

    
    if (!$v->check($post)) {
        $this->error('登录失败：' . $v->getError());
    }

    $u = model('admin/User')->where('openid',$post["openid"])->find();
    if($u){
      // 执行登录
      Token::clear($u["id"]);
      $this->auth->direct($u["id"]);
      $this->success('登录成功', $this->auth->getUserinfo());
    }
    else{
      // 执行注册
      $username = $post["openid"];
      // 初始密码给一个随机数
      $password = Random::alnum(15);
      $this->auth->register($username,$password,'','',[
          "openid"=>$post["openid"]
      ]);
      $this->success('注册成功', $this->auth->getUserinfo());

    }
  }

  public function ceshi()
  {
    // 16250dbc-2548-4350-bbec-325e28eec334
    $this->success('成功', $this->auth->getUserinfo());
  }
}